<?php
    defined('INDEX') or die ('Acesso direto n&atilde;o permitido');
    
	class DoctrineConnection
	{
		private $modelDirectory = "";
		
		public function __construct()
		{
			require_once (dirname(__FILE__)."/../application/Application.class.php");
			$config = ApplicationFactory::getConfigurationFactory();
			
			require_once (dirname(__FILE__)."/Doctrine.php");
	 		
			spl_autoload_register(array('Doctrine', 'autoload'));
			spl_autoload_register(array('Doctrine_Core', 'modelsAutoload'));
			
			$manager = Doctrine_Manager::getInstance();
			
			try
			{
				$this->modelDirectory = dirname(__FILE__)."/../../model/";
				$str  = "mysql://".$config->getDatabaseUser().":".
				  			$config->getDatabasePassword()."@".
				  			$config->getDatabaseServer()."/".$config->getDatabaseName();
				  			
				$conn = Doctrine_Manager::connection($str);
				 
				$manager->setAttribute(Doctrine_Core::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_CONSERVATIVE); 
				$manager->setAttribute(Doctrine_Core::ATTR_EXPORT, Doctrine_Core::EXPORT_ALL);
				 
				$profiler = new Doctrine_Connection_Profiler();
				$manager->setListener($profiler);
			} catch (Doctrine_Manager_Exception $e)
			{
				die($e->getMessage());
			}
			 
			Doctrine_Core::loadModels($this->modelDirectory);
		}
		
		/**
		 * Returns the models directory.
		 * 
		 * @return string $modelDirectory
		 */
		public function getModelDirectory()
		{
			return $this->modelDirectory;
		}
	}
?>